package palmtop.com.palmtop.activity;

import android.app.DatePickerDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.style.FontStyle;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import net.grandcentrix.tray.AppPreferences;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.model.Cluecount;
import palmtop.com.palmtop.model.Examine;
import palmtop.com.palmtop.model.Populationcount;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.NetUtil;

public class KaoheActivity extends BaseActivity {

    @BindView(R.id.table)
    SmartTable table;

    @BindView(R.id.table1)
    SmartTable table1;

    @BindView(R.id.table2)
    SmartTable table2;


    String ssgrid;
    String role_id;
    @BindView(R.id.lay)
    LinearLayout lay;
    @BindView(R.id.lay1)
    LinearLayout lay1;
    @BindView(R.id.lay2)
    LinearLayout lay2;
    @BindView(R.id.startdate)
    EditText startdate;
    @BindView(R.id.enddate)
    EditText enddate;
    @BindView(R.id.search)
    Button search;

    int mYear = 0;
    int mMonth = 0;
    int mDay = 0;

    int mYear1 = 0;
    int mMonth1 = 0;
    int mDay1 = 0;
    DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            String days;
            if (mMonth + 1 < 10) {
                if (mDay < 10) {
                    days = new StringBuffer().append(mYear).append("-").append("0").
                            append(mMonth + 1).append("-").append("0").append(mDay).append("").toString();
                } else {
                    days = new StringBuffer().append(mYear).append("-").append("0").
                            append(mMonth + 1).append("-").append(mDay).append("").toString();
                }
            } else {
                if (mDay < 10) {
                    days = new StringBuffer().append(mYear).append("-").
                            append(mMonth + 1).append("-").append("0").append(mDay).append("").toString();
                } else {
                    days = new StringBuffer().append(mYear).append("-").
                            append(mMonth + 1).append("-").append(mDay).append("").toString();
                }

            }
            startdate.setText(days);
        }
    };

    DatePickerDialog.OnDateSetListener onDateSetListener1 = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            mYear1 = year;
            mMonth1 = monthOfYear;
            mDay1 = dayOfMonth;
            String days;
            if (mMonth1 + 1 < 10) {
                if (mDay1 < 10) {
                    days = new StringBuffer().append(mYear1).append("-").append("0").
                            append(mMonth1 + 1).append("-").append("0").append(mDay1).append("").toString();
                } else {
                    days = new StringBuffer().append(mYear1).append("-").append("0").
                            append(mMonth1 + 1).append("-").append(mDay1).append("").toString();
                }
            } else {
                if (mDay1 < 10) {
                    days = new StringBuffer().append(mYear1).append("-").
                            append(mMonth1 + 1).append("-").append("0").append(mDay1).append("").toString();
                } else {
                    days = new StringBuffer().append(mYear1).append("-").
                            append(mMonth1 + 1).append("-").append(mDay1).append("").toString();
                }

            }
            enddate.setText(days);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kaohe, Config.toptitle, 1);
        MyApplication app = (MyApplication) getApplication();
        app.addActivityList(KaoheActivity.this);
        ButterKnife.bind(this);
        AppPreferences appPreferences = new AppPreferences(KaoheActivity.this);
        ssgrid = appPreferences.getString("ssgrid", "");
        role_id = appPreferences.getString("role_id", "");
        initView();
    }

    private void initView() {
        Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);

        mYear1 = ca.get(Calendar.YEAR);
        mMonth1 = ca.get(Calendar.MONTH);
        mDay1 = ca.get(Calendar.DAY_OF_MONTH);
        startdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startdate.setInputType(InputType.TYPE_NULL);
                new DatePickerDialog(KaoheActivity.this, onDateSetListener, mYear, mMonth, mDay).show();
            }
        });
        enddate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                enddate.setInputType(InputType.TYPE_NULL);
                new DatePickerDialog(KaoheActivity.this, onDateSetListener1, mYear1, mMonth1, mDay1).show();
            }
        });

        //任务统计====================================================================================
        RequestParams params = new RequestParams(Config.SERVER_URL + "taskgridapp/analisys");
        if (role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")) {
            params.addBodyParameter("unitid", ssgrid);
        }
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!result.equals("[]")) {
                    Gson gson = new Gson();
                    Type listType = new TypeToken<ArrayList<Examine>>() {
                    }.getType();
                    final ArrayList<Examine> list = gson.fromJson(result, listType);
                    for (int i = 0; i < list.size(); i++) {
                        Examine examine = list.get(i);
                        if (examine.getSum1() == null) {
                            examine.setSum1("0");
                        }
                        if (examine.getSum2() == null) {
                            examine.setSum2("0");
                        }
                        if (examine.getSum3() == null) {
                            examine.setSum3("0");
                        }
                    }
                    table.setData(list);
                    table.getConfig().setContentStyle(new FontStyle(50, Color.BLUE));
                } else {
                    lay.setVisibility(View.GONE);
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
        //线索统计========================================================================
        RequestParams params1 = new RequestParams(Config.SERVER_URL + "clue/analisys");
        if (role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")) {
            params1.addBodyParameter("unitid", ssgrid);
        }
        NetUtil.postData(params1, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!result.equals("[]")) {
                    Gson gson = new Gson();
                    Type listType = new TypeToken<ArrayList<Cluecount>>() {
                    }.getType();
                    final ArrayList<Cluecount> list = gson.fromJson(result, listType);
                    table1.setData(list);
                    table1.getConfig().setContentStyle(new FontStyle(50, Color.BLUE));
                } else {
                    lay1.setVisibility(View.GONE);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });

        //基础信息统计========================================================================
        RequestParams params2 = new RequestParams(Config.SERVER_URL + "reportpopulation/analisys");
        if (role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")) {
            params2.addBodyParameter("unitid", ssgrid);
        }
        NetUtil.postData(params2, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                if (!result.equals("[]")) {
                    Gson gson = new Gson();
                    Type listType = new TypeToken<ArrayList<Populationcount>>() {
                    }.getType();
                    final ArrayList<Populationcount> list = gson.fromJson(result, listType);
                    table2.setData(list);
                    table2.getConfig().setContentStyle(new FontStyle(50, Color.BLUE));
                } else {
                    lay2.setVisibility(View.GONE);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    @OnClick(R.id.search)
    public void onViewClicked() {
        //任务统计====================================================================================
        RequestParams params = new RequestParams(Config.SERVER_URL + "taskgridapp/analisys");
        if (role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")) {
            params.addBodyParameter("unitid", ssgrid);
        }
        if (!TextUtils.isEmpty(startdate.getText())) {
            params.addBodyParameter("startdate",startdate.getText().toString());
        }
        if (!TextUtils.isEmpty(enddate.getText())) {
            params.addBodyParameter("enddate",enddate.getText().toString());
        }
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!result.equals("[]")) {
                    Gson gson = new Gson();
                    Type listType = new TypeToken<ArrayList<Examine>>() {
                    }.getType();
                    final ArrayList<Examine> list = gson.fromJson(result, listType);
                    for (int i = 0; i < list.size(); i++) {
                        Examine examine = list.get(i);
                        if (examine.getSum1() == null) {
                            examine.setSum1("0");
                        }
                        if (examine.getSum2() == null) {
                            examine.setSum2("0");
                        }
                        if (examine.getSum3() == null) {
                            examine.setSum3("0");
                        }
                    }
                    table.setData(list);
                    table.getConfig().setContentStyle(new FontStyle(50, Color.BLUE));
                } else {
                    lay.setVisibility(View.GONE);
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
        //线索统计========================================================================
        RequestParams params1 = new RequestParams(Config.SERVER_URL + "clue/analisys");
        if (role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")) {
            params1.addBodyParameter("unitid", ssgrid);
        }
        if (!TextUtils.isEmpty(startdate.getText())) {
            params1.addBodyParameter("startdate",startdate.getText().toString());
        }
        if (!TextUtils.isEmpty(enddate.getText())) {
            params1.addBodyParameter("enddate",enddate.getText().toString());
        }
        NetUtil.postData(params1, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!result.equals("[]")) {
                    Gson gson = new Gson();
                    Type listType = new TypeToken<ArrayList<Cluecount>>() {
                    }.getType();
                    final ArrayList<Cluecount> list = gson.fromJson(result, listType);
                    table1.setData(list);
                    table1.getConfig().setContentStyle(new FontStyle(50, Color.BLUE));
                } else {
                    lay1.setVisibility(View.GONE);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });

        //基础信息统计========================================================================
        RequestParams params2 = new RequestParams(Config.SERVER_URL + "reportpopulation/analisys");
        if (role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")) {
            params2.addBodyParameter("unitid", ssgrid);
        }
       if (!TextUtils.isEmpty(startdate.getText())) {
            params2.addBodyParameter("startdate",startdate.getText().toString());
        }
        if (!TextUtils.isEmpty(enddate.getText())) {
            params2.addBodyParameter("enddate", enddate.getText().toString());
        }
        NetUtil.postData(params2, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                if (!result.equals("[]")) {
                    Gson gson = new Gson();
                    Type listType = new TypeToken<ArrayList<Populationcount>>() {
                    }.getType();
                    final ArrayList<Populationcount> list = gson.fromJson(result, listType);
                    table2.setData(list);
                    table2.getConfig().setContentStyle(new FontStyle(50, Color.BLUE));
                } else {
                    lay2.setVisibility(View.GONE);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }


}
